<?php
namespace phpcodehub\test;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/16 0016
 * Time: 上午 9:22
 */

use phpcodehub\files\FileUpload;

include_once '../file/FileUpload.php';

class Test
{
    public function __construct()
    {

    }

    public function fileUploadTest() {
        if ( $_FILES ) {
            $a = new FileUpload();
            $a->set( 'path', '../data' );
            if ( $a->upload( 'files' ) ) {
                echo 'success';
            }
            else {
                var_dump($a->getErrorMessage());
            }
        }
    }

    public function test() {
        $string = '03-04-01-07-05';

        $count = ( strlen($string) + 1 ) / 3;
        $newstring = '';
        for ( $i = 0; $i < $count; $i++ ) {
            $number = substr($string, $i * 3, 2 );
            $count = ( strlen($string) + 1 ) / 3;
//            echo '<br>'.$number . '<br>';
            $k = 0;
            for ( $j = $i + 1; $j < $count; $j++ ) {

                $a = substr($string, $j * 3, 2 );

                if ( $number > $a ) {
                    $k = $j * 3;
                    $number = $a;
                }
            }
            //echo $k . '<br>';
            $string1 = substr($string, 0, $k - 1);
            $string2 = substr($string, $k + 2, strlen($string) - $k - 2);
            if ( $k === 0 ) {
                $string1 = '';
                $string2 = substr($string, $k + 3, strlen($string) - $k - 1);
            }

            //echo $string1 . '<br>';
            //echo $string2 . '<br>';
            $string = $string1 . $string2;
            echo $string . '<br>';

            $newstring .= $number . '-';
        }
        echo $newstring.'<br>';
    }

    public function min( $string ) {
        $number = substr($string, 0, 2 );
        $k = 0;
        for ( $j = 1; $j < (strlen($string) + 1) / 3; $j++ ) {
            $a = substr($string, $j * 3, 2 );
            if ( $number > $a ) {
                $k = $j * 3;
                $number = $a;
            }
        }
        return substr($string, $k, 2 );
    }

    public function main( $string ) {
        $newstring = '';
        $count = ( strlen($string) + 1 ) / 3;

        for ( $i = 0; $i < $count; $i++ ) {
            $newstring .= $this->min($string) . '-';
            $string = $this->changeString($string);
        }
        $newstring = substr($newstring, 0, strlen($newstring) - 1 );
        echo $newstring;

    }

    public function changeString( $string ) {

            $number = substr($string, 0, 2 );
//            echo '<br>'.$number . '<br>';
            $k = 0;
            for ( $j = 1; $j < (strlen($string) + 1) / 3; $j++ ) {
                $a = substr($string, $j * 3, 2 );
                if ( $number > $a ) {
                    $k = $j * 3;
                    $number = $a;
                }
            }

        $string1 = substr($string, 0, $k - 1);
        $string2 = substr($string, $k + 2, strlen($string) - $k - 2);
        if ( $k === 0 ) {
            $string1 = '';
            $string2 = substr($string, $k + 3, strlen($string) - $k - 1);
        }

        $string = $string1 . $string2;
        return $string ;

    }


    public function combine( $string ) {
        $count = (strlen($string) + 1 ) / 3;

        for ( $i = 0; $i < $count; $i++ ) {
            $a = substr($string, $i * 3, 2 );
            $com = '';
            for ( $j = $i + 1; $j < $count; $j++ ) {
                $b = substr($string, $j * 3, 2 );
                $com = $a . $b;
                echo $com . '<br>';
            }
        }
    }

    public function combine3( $string ) {
        $count = (strlen($string) + 1 ) / 3;
        $num = 0;
        for ( $i = 0; $i < $count - 2; $i++ ) {
            $a = substr($string, $i * 3, 2 );
            for ( $j = $i + 1; $j < $count - 1; $j++ ) {
                $b = $a . substr($string, $j * 3, 2 );
                for ( $k = $j + 1; $k < $count; $k++ ) {
                    $c = $b . substr($string, $k * 3, 2 );
                    echo $c .'<br>';
                    $num++;
                }
            }
        }
        echo $num;
    }

    public function combine4( $string ) {
        $count = (strlen($string) + 1 ) / 3;
        $num = 0;
        for ( $h = 0; $h < $count - 3; $h++ ) {
            $e = substr($string, $h * 3, 2);
            for ($i = $h + 1; $i < $count - 2; $i++) {
                $a = $e . substr($string, $i * 3, 2);
                for ($j = $i + 1; $j < $count - 1; $j++) {
                    $b = $a . substr($string, $j * 3, 2);
                    for ($k = $j + 1; $k < $count; $k++) {
                        $c = $b . substr($string, $k * 3, 2);
                        echo $c . '<br>';
                        $num++;
                    }
                }
            }
        }
        echo $num;
    } 

    public function testDiGui( $string, $n ) {
        $count = (strlen($string) + 1 ) / 3;
        if ( $count < 5 ) {
            return $string;
        }
        $this->digui( 0, $count, $n, '', $string );
    }

    public function digui( $a, $count, $n, $s, $string ) {
        if ( $n - 1 === 0 ) {
            echo $s . '<br>';
            return;
        }
        for ( $b = $a + 1; $b < $count - $n + 1; $b++ ) {
            $s = $s . substr($string, $b * 3, 2);
            $this->digui( $b, $count, $n--, $s, $string );
        }
    }

    public function combine5( $string ) {
        $count = (strlen($string) + 1 ) / 3;
        if ( $count < 5 ) {
            return $string;
        }
        $num = 0;
        for ( $g = 0; $g < $count - 4; $g++ ) {
            $f = substr($string, $g * 3, 2);
            for ($h = $g + 1; $h < $count - 3; $h++) {
                $e = $f . substr($string, $h * 3, 2);
                for ($i = $h + 1; $i < $count - 2; $i++) {
                    $a = $e . substr($string, $i * 3, 2);
                    for ($j = $i + 1; $j < $count - 1; $j++) {
                        $b = $a . substr($string, $j * 3, 2);
                        for ($k = $j + 1; $k < $count; $k++) {
                            $c = $b . substr($string, $k * 3, 2);
                            echo $c . '<br>';
                            $num++;
                        }
                    }
                }
            }
        }
        echo $num;
    }

    public function com( $string, $n ) {
        $count = (strlen($string) + 1 ) / 3;
        if ( $n === 1 ) {
            for ( $i = 0; $i < $count - 2; $i++ ) {
                $a = substr($string, $i * 3, 2 );
            }
        }

    }

    public function comb($string, $len) {

    }

    public function repeatNum( array $number ) {
        $length = count($number);
        $data = [];
        if ( $number == null || $length <= 0 ) {
            return false;
        }

        for ( $i = 0; $i < $length; $i++ ) {
            if ( $number[ $i ] < 0 || $number[ $i ] > $length - 1 ) {
                return false;
            }
        }

        for ( $i = 0; $i < $length; $i++ ) {
            while ( $number[ $i ] != $i ) {
                if ( $number[ $i ] == $number[ $number[ $i ] ] ) {
                    $data[] = $number[$i];
                    return true;
                }

                $number[ $i ] ^= $number[ $number[ $i ] ];
                $number[ $number[$i] ] ^= $number[ $i ];
                $number[ $i ] ^= $number[ $number[ $i ] ];
            }
        }
        if ( $data === [] )
            return false;
        else {
            return $data;
        }
    }

    public function bubbleSort( array $num ) {
        if ( $num == null ) {
            return $num;
        }
        $count = 0;
        $len = count($num);

        for ( $i = 0; $i < $len; $i++ ) {
            for ( $j = $i + 1; $j < $len; $j++ ) {
                if ( $num[ $j ] < $num[ $i ] ) {
                    $num[$j] ^= $num[$i];
                    $num[$i] ^= $num[$j];
                    $num[$j] ^= $num[$i];
                }
                $count++;
            }
        }
        echo $count;
        return $num;
    }

    public function getDuplication(array $number) {
        $len = count($number);

        if ( $number == null || $len <= 0 ) {
            return -1;
        }
        $start = 1;
        $end = $len - 1;
        while ( $end >= $start ) {
            $middle = (( $end - $start ) >> 1) + $start;
            $count = $this->countRange( $number, $len, $start, $middle );

            if ( $end == $start ) {
                if ( $count > 1 ) {
                    return $start;
                }
                else{
                    break;
                }
            }

            if ( $count > ( $middle - $start + 1 ) ) {
                $end = $middle;
            }
            else {
                $start = $middle + 1;
            }
        }
        return -1;
    }

    private function countRange( $numbers, $length, $start, $end ) {
        if ( $numbers == null ) {
            return 0;
        }

        $count = 0;
        for ( $i = 0; $i < $length; $i++ ) {
            if ( $numbers[ $i ] >= $start && $numbers[ $i ] <= $end ) {
                ++$count;
            }
        }
        return $count;
    }

    public function find(array $multiArray, $target) {
        $found = false;

        if ( $multiArray != null ) {

            $rows = count($multiArray);
            $columns = count($multiArray[0]);
            foreach ($multiArray as $item) {
                if ( count($item) !== $columns) {
                    return false;
                }
            }

            $row = 0;
            $column = $columns - 1;
            while ($row < $rows && $column >= 0) {
                if ( $multiArray[$row][$column] === $target ) {
                    $found = true;
                    break;
                }
                else if ( $multiArray[$row][$column] > $target) {
                    --$column;
                }
                else {
                    ++$row;
                }
            }

        }

        return $found;
    }

    public function feibo( $n ) {
        if ( $n === 0 ) {
            return 0;
        }
        if ( $n === 1 ) {
            return 1;
        }

        return $this->feibo( $n - 1 ) + $this->feibo( $n - 2 );
    }

    public function xfeibo( $n ) {
        $result = [0,1];
        if ( $n < 2 ) {
            return $result[$n];
        }

        $fibNMinusOne = 1;
        $fibNMinusTwo = 0;

        $fibN = 0;
        for ( $i = 2; $i <= $n; ++$i ) {
            $fibN = $fibNMinusOne + $fibNMinusTwo;

            $fibNMinusTwo = $fibNMinusOne;
            $fibNMinusOne = $fibN;
        }

        return $fibN;
    }
}

$a = new Test();

// $b = [
//     [1,2,8,9],
//     [2,4,9,12],
//     [4,7,10,13],
//     [6,8,11,15]
// ];


// var_dump($a->find($b,7));

//$a->combine5('01-02-03-04-05-06', 5);

echo $a->xfeibo(100);